<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>车间基础建模</el-breadcrumb-item>
      <el-breadcrumb-item>工作中心</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="7">
          <el-input placeholder="请输入工作中心编号检索" v-model="query">
            <template #append>
              <el-button icon="el-icon-search" @click="getList">检索</el-button>
            </template>
          </el-input>
        </el-col>
        <el-col span="4">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addDialogVisible = true"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="workcenterList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="id" width="120" align="center">
        </el-table-column>
        <el-table-column
          prop="workcenter_no"
          label="工作中心编号"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_name"
          label="工作中心名称"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_description"
          label="工作中心描述"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_class"
          label="工作中心层级"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column label="工作中心类型" width="120" align="center">
          <template #default="scope">
            {{ whatType(scope.row.workcenter_class) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="workcenter_version"
          label="工作中心版本"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_parent"
          label="父工作中心"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="site"
          label="挂靠站点"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="byUser"
          label="创建人"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column label="创建时间" width="200" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
    </el-card>
    
    <!-- 添加对话框 -->
    <el-dialog title="新增工作中心" v-model="addDialogVisible" width="40%">
      <!-- 二级对话框 -->
    <el-dialog title="工作中心列表" v-model="faDialogVisible" width="40%" append-to-body>
      <!-- 对话框内容 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="workcenterList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="small"
      >
        <el-table-column prop="id" label="id" width="auto" align="center">
        </el-table-column>
        <el-table-column
          prop="workcenter_no"
          label="工作中心编号"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_name"
          label="工作中心名称"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_description"
          label="工作中心描述"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="choiceAdd(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="faDialogVisible = false">取 消</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 二级对话框结束 -->
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="120px"
      >
        <el-form-item label="所属站点" prop="site">
          <el-input v-model="addForm.site" disabled placeholder="请选择站点"></el-input>
        </el-form-item>
        <el-form-item label="工作中心编号" prop="workcenter_no">
          <el-input v-model="addForm.workcenter_no" placeholder="请输入工作中心编号"></el-input>
        </el-form-item>
        <el-form-item label="工作中心名称" prop="workcenter_name">
          <el-input v-model="addForm.workcenter_name" placeholder="请输入工作中心名称"></el-input>
        </el-form-item>
        <el-form-item label="工作中心描述" prop="workcenter_description">
          <el-input v-model="addForm.workcenter_description" placeholder="请输入工作中心描述"></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="workcenter_version">
          <el-select
            v-model="addForm.workcenter_version"
            placeholder="请选择版本"
            
          >
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心层级" prop="workcenter_class">
          <el-select
            v-model="addForm.workcenter_class"
            placeholder="请选择工作中心层级"
            
          >
            <el-option label="车间" value="workshop"></el-option>
            <el-option label="生产线" value="production_line"></el-option>
            <el-option label="工位" value="station"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心类型" prop="workcenter_type">
          <el-select
            v-model="addForm.workcenter_type"
            placeholder="请选择工作中心类型"
            
          >
            <el-option label="装配" value="assembly"></el-option>
            <el-option label="调度" value="schedule"></el-option>
            <el-option label="装运" value="ship"></el-option>
            <el-option label="转移" value="shift"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父工作中心" prop="workcenter_parent">
          <el-input
            v-model="addForm.workcenter_parent"
            placeholder="请选择父工作中心"
          >
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="faDialogVisible=true"></i>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-select
            v-model="addForm.state"
            placeholder="请选择是否可用"
            
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add" 
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑工作中心" v-model="editDialogVisible" width="40%">
      <!-- 二级对话框 -->
    <el-dialog title="工作中心列表" v-model="faDialogVisible" width="40%" append-to-body>
      <!-- 对话框内容 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="workcenterList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="id" width="auto" align="center">
        </el-table-column>
        <el-table-column
          prop="workcenter_no"
          label="工作中心编号"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_name"
          label="工作中心名称"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="workcenter_description"
          label="工作中心描述"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              @click="choiceEdit(scope.row)"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="faDialogVisible = false">取 消</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 二级对话框结束 -->
      <!-- 对话框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="120px"
      >
        <el-form-item label="所属站点" prop="station">
          <el-input v-model="editForm.site" disabled placeholder="请选择所属站点"></el-input>
        </el-form-item>
        <el-form-item label="工作中心编号" prop="no">
          <el-input v-model="editForm.workcenter_no" placeholder="请输入工作中心编号"></el-input>
        </el-form-item>
        <el-form-item label="工作中心名称" prop="name">
          <el-input v-model="editForm.workcenter_name" placeholder="请输入工作中心名称"></el-input>
        </el-form-item>
        <el-form-item label="工作中心描述" prop="dec">
          <el-input v-model="editForm.workcenter_description" placeholder="请输入工作中心描述"></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-select
            v-model="editForm.workcenter_version"
            placeholder="请选择版本"
            
          >
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心层级" prop="class">
          <el-select
            v-model="editForm.workcenter_class"
            placeholder="请选择工作中心层级"
            
          >
            <el-option label="车间" value="workshop"></el-option>
            <el-option label="生产线" value="production_line"></el-option>
            <el-option label="工位" value="station"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心类型"  prop="type">
          <el-select
            v-model="editForm.workcenter_type"
            placeholder="请选择工作中心类型"
           
          >
            <el-option label="装配" value="assembly"></el-option>
            <el-option label="调度" value="schedule"></el-option>
            <el-option label="装运" value="ship"></el-option>
            <el-option label="转移" value="shift"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父工作中心" prop="center">
          <el-input
            v-model="editForm.workcenter_parent"
            placeholder="请选择父工作中心"
          >
          <template #suffix>
          <i class="el-input__icon el-icon-search" @click="faDialogVisible=true"></i>
          </template></el-input>
        </el-form-item>
        <el-form-item label="是否可用" prop="isUseable">
          <el-select
            v-model="editForm.state"
            placeholder="请选择是否可用"
            
          >
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit" 
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      query: "",
      pageCount: 1,
      pageNow: 1,
      pageSize: 10,
      rowCount: 0,
      workcenterList: [],
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框 
      editDialogVisible: false,
      // 显示二级对话框
      faDialogVisible: false,
      addForm: {
        site: "*",
        workcenter_no: "",
        workcenter_name: "",
        workcenter_description: "",
        workcenter_version: "",
        workcenter_class: "",
        workcenter_type: "",
        workcenter_parent: "",
        state: "",
      },
      editForm:{},
      addFormrules: {
        site: [],
        workcenter_no: [
          {
            required: true,
            message: "请输入工作中心编号",
            trigger: "blur",
          },
        ],
        workcenter_name: [
          {
            required: true,
            message: "请输入工作中心名称",
            trigger: "blur",
          },
        ],
        workcenter_description: [
          {
            required: true,
            message: "请输入工作中心描述",
            trigger: "blur",
          },
        ],
        workcenter_version: [
          {
            required: true,
            message: "请选择版本",
            trigger: "blur",
          },
        ],
        workcenter_class: [
          {
            required: true,
            message: "请选择工作中心层级",
            trigger: "blur",
          },
        ],
        workcenter_type: [
          {
            required: true,
            message: "请选择工作中心类型",
            trigger: "blur",
          },
        ],
        workcenter_parent: [
          {
            required: true,
            message: "请输入父工作中心",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择是否可用",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$axios({
        headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
        method: "post",
        url: "/IE_MES/workshop/workcenter_findByPage.shtml",
        data: this.$qs.stringify({
              "pageSize":this.pageSize,
              "pageNow": this.pageNow,
              "workcenterFormMap.workcenter_no": this.query
            }),
      })
        .then((res) => {
          console.log(res.data);
          this.workcenterList = res.data.records;
          this.pageCount = res.data.pageCount;
          this.pageNow = res.data.pageNow;
          this.pageSize = res.data.pageSize;
          this.rowCount = res.data.rowCount;
        });
    },
    // 新增
    add() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/workshop/workcenter_addEntity.shtml",
            data: this.$qs.stringify({
              "workcenterFormMap.site":this.addForm.site,
              "workcenterFormMap.workcenter_no":this.addForm.workcenter_no,
              "workcenterFormMap.workcenter_name":this.addForm.workcenter_name,
              "workcenterFormMap.workcenter_description":this.addForm.workcenter_description,
              "workcenterFormMap.workcenter_version":this.addForm.workcenter_version,
              "workcenterFormMap.workcenter_class":this.addForm.workcenter_class,
              "workcenterFormMap.workcenter_type":this.addForm.workcenter_type,
              "workcenterFormMap.workcenter_parent":this.addForm.workcenter_parent,
              "workcenterFormMap.state":this.addForm.state,
            }),
      }).then((res) => {
            if (res.data == "{\"result\":\"success\",\"score\":\"\"}") {
              this.getList();
              this.$refs['addFormRef'].resetFields();
              this.addDialogVisible=false;
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            }
          });
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.editDialogVisible = true;
    },
    edit() {
      this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/workshop/workcenter_editEntity.shtml",
            data: this.$qs.stringify({
              "workcenterFormMap.id":this.editForm.id,
              "workcenterFormMap.site":this.editForm.site,
              "workcenterFormMap.workcenter_no":this.editForm.workcenter_no,
              "workcenterFormMap.workcenter_name":this.editForm.workcenter_name,
              "workcenterFormMap.workcenter_description":this.editForm.workcenter_description,
              "workcenterFormMap.workcenter_version":this.editForm.workcenter_version,
              "workcenterFormMap.workcenter_class":this.editForm.workcenter_class,
              "workcenterFormMap.workcenter_type":this.editForm.workcenter_type,
              "workcenterFormMap.workcenter_parent":this.editForm.workcenter_parent,
              "workcenterFormMap.state":this.editForm.state,
            }),
      }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.editDialogVisible=false;
              this.$message({
                type: "success",
                message: "编辑成功!",
              });
            }
          });
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该巡检内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/workshop/workcenter_deleteEntity.shtml?_t="+new Date(),
            data: this.$qs.stringify({
              ids:row.id
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 二级对话框选择功能
    choiceAdd(row) {
      this.addForm.workcenter_parent = row.workcenter_no;
      this.faDialogVisible = false;
    },
    choiceEdit(row) {
      this.editForm.workcenter_parent = row.workcenter_no;
      this.faDialogVisible = false;
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.pageNow = newPage;
      this.getList();
    },
    whatType(type) {
      if (type == "workshop") {
        return "车间";
      } else if (type == "production_line") {
        return "产线 ";
      } else {
        return "工位";
      }
    },
    formatDate(data) {
      var date = new Date(data);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
